/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.prashant.sqlduniya.sqloutput;

import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import org.prashant.sqlduniya.sqlmanager.SQLResult;

/**
 *
 * @author prashant
 */
public class TableNodeModel extends AbstractTableModel {

    private String[] columns = {};
    private List<List<String>> data = new ArrayList<List<String>>();
    
 
    public TableNodeModel(SQLResult result) {
        int cols = result.getTable().getColumns().size();
        columns = new String[cols];
        for(int i=0; i< cols; i++) {
            columns[i] = result.getTable().getColumns().get(i).getLabel();
        }
        this.data = result.getResult();
    }
    
    public List<String> getRow(int row) {
        return data.get(row);
    }

    @Override
    public int getRowCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return columnIndex == 0? Integer.class : String.class;
    }

    @Override
    public String getColumnName(int colIndex) {
        return columns[colIndex];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) { 
        return data.get(rowIndex).get(columnIndex);
    }
}
